"""
Case Type   : reindex
Case Name   : 使用reindex在线对ustore表重建索引,合理报错
Create At   : 2022/08/31
Owner       : liu-tong-8848
Description :
    1.建表
    2.向表插入数据
    3.创建索引
    4.执行reindex table concurrently
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.dbuserNode = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.table = 't_reindex_concurrently_0068'
        self.index = 'i_reindex_concurrently_0068'
        self.Constant = Constant()

    def test_reindex(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = 'step1:建表 expect:成功'
        self.logger.info(step1)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table};
            create table {self.table}(c_int int,col_text text) 
            with (storage_type=ustore);''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + step1)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "建表1失败" + step1)

        step2 = 'step2:向表1中插入数据 expect:成功'
        self.logger.info(step2)
        insert_data = self.primary_sh.execut_db_sql(
            f'''insert into {self.table} values(generate_series(1,500000),
            'test text');''')
        self.logger.info(insert_data)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, insert_data,
                      "向表1中插入数据失败" + step2)

        step3 = 'step3:创建索引 expect:成功'
        self.logger.info(step3)
        create_index = self.primary_sh.execut_db_sql(
            f'''drop index if exists {self.index};
            create index {self.index} on {self.table}(c_int);''')
        self.logger.info(create_index)
        self.assertIn(self.Constant.DROP_INDEX_SUCCESS_MSG, create_index,
                      "执行失败" + step3)
        self.assertIn(self.Constant.CREATE_INDEX_SUCCESS_MSG, create_index,
                      "执行失败" + step3)

        step4 = 'step4:执行reindex table concurrently' \
                'expect:合理报错'
        self.logger.info(step4)
        reindex = self.primary_sh.execut_db_sql(
            f'''reindex table concurrently {self.table};''')
        self.logger.info(reindex)
        self.assertIn('ERROR:  concurrent reindex is not supported for ustore '
                      'table', reindex, "执行失败" + step4)

    def tearDown(self):
        step5 = 'step5:清理环境 expect:成功'
        self.logger.info(step5)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table} cascade;''')
        self.logger.info(clean_environment)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, clean_environment,
                      '执行失败' + step5)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
